home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6454 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: aborting a constructor
  5. Date: 8 Feb 1996 14:50:37 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4fd2jt$hf3@dawn.mmm.com>
  8. References: <310DA3AB.6E32@tribeca.ios.com> <4f8eo6$kik@druid.borland.com>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Pete Becker (pete@borland.com) wrote:
  13. > In article <310DA3AB.6E32@tribeca.ios.com>, leonardj@tribeca.ios.com says...
  14. > >
  15. > >In an earlier post, I asked how you use 'new' and a constructor in order to 
  16. > deal with the 
  17. > >following situation:
  18. > >     You have a class, let's call it WindowClass, which, when it is 
  19. > instantiated, does the 
  20. > >following:
  21. > >     1: It opens the file "filename", and gets its size.
  22. > >     2: It allocates memory to hold "filename".
  23. > >     3: It loads "filename" into that memory.
  24. > >     4: It opens a window to display "filename".
  25. > >     Now let's suppose that after it performs steps 1 thru 3 it cannot open 
  26. > the window, what does it 
  27. > >do? Furthermore, how do you code for this kind of situation in C++ in 
  28. > general? My first thought was 
  29. > >that the constructor should de-allocate all of the resources that it had 
  30. > received so far. Then it 
  31. > >should throw an exception. The question that I had was: what's going to 
  32. > happen to the memory that 
  33. > >was allocated for the object's data members? 
  34.  
  35. > The compiler is responsible for generating code that will release allocated 
  36. > memory when a constructor throws an exception.
  37.  
  38. > >I nother words: what happens to an object if its 
  39. > >constructor aborts?
  40.  
  41. > This is an entirely different question, and its answer depends on what you 
  42. > mean by "aborts".
  43.  
  44. If by "abort" you mean "throws an exception," then the following will happen:
  45.     1) Any fully constructed sub-objects will be destroyed, and
  46.     2) If the object was allocated in a new-expression, the storage for the
  47.        object will be deleted also (if placement-new was not used).
  48.  
  49. So in your example above, if step 4 fails, the memory allocated for the object
  50. itself will be released, but the memory allocated in step 2 will not be
  51. released unless the destructor of a fully constructed sub-object releases it.
  52. The same is true for the file opened in step 1 -- it will not be closed
  53. automatically.
  54. --
  55. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  56. 3M Company                      phone:  (612) 737-4643
  57. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  58. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  59.     But 3M speaks for me -- I did not write the following line:
  60.  
  61. Opinions expressed herein are my own and may not represent those of 3M.
  62.